package com.elinkway.infinitemovies.view;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.elinkway.infinitemovies.adaction.CommonAdActionHandle;
import com.elinkway.infinitemovies.async.ThirdAdReportTask;
import com.elinkway.infinitemovies.bean.HzAdBean;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Author: liuxiaojiang(xiao)
 * Date: 2017-06-05
 * Time: 14:57
 * Description: 互众广告展示的view
 */
public class HzPayAdView {
    private Activity activity;
    private View mView;
    private long startRequestTime = 0;
    private String mAdposition;
    ArrayList<String> adShowReportUrls = null;
    ArrayList<String> adClickReportUrls = null;
    ArrayList<String> adClickDpReportUrls = null;

    public HzPayAdView(Activity activity, View view, String mAdposition) {
        this.activity = activity;
        this.mView = view;
        this.mAdposition = mAdposition;
    }

    public HzPayAdView(Activity activity, String mAdposition) {
        this.activity = activity;
        this.mAdposition = mAdposition;
    }

    public void setHzAd(final HzAdBean hzAdBean) {
        if (mView == null) {
            mView = activity.findViewById(R.id.nativeADContainerone);
        }

        adShowReportUrls = hzAdBean.imp;
        adClickReportUrls = hzAdBean.clk;
        adClickDpReportUrls = hzAdBean.dp_clk;

        AQuery aq = new AQuery(activity, mView);
        mView.setVisibility(View.VISIBLE);
        if ("pause".equals(mAdposition)) {
            mView.findViewById(R.id.pause_ad_image).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_close_iv).setVisibility(View.VISIBLE);
            mView.findViewById(R.id.ad_logo_iv).setVisibility(View.GONE);
            TextView tipsTv = (TextView) mView.findViewById(R.id.ad_tips_tv);
            RelativeLayout.LayoutParams tipsParams = (RelativeLayout.LayoutParams) tipsTv.getLayoutParams();
            tipsParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.pause_ad_image);
            tipsTv.setLayoutParams(tipsParams);
            tipsTv.setVisibility(View.VISIBLE);
            mView.findViewById(R.id.baidu_inter_ad_rl).setVisibility(View.GONE);
            aq.id(R.id.pause_ad_image).image(hzAdBean.src);
            mView.findViewById(R.id.ad_close_iv).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mView.setVisibility(View.GONE);
                    reportAdClose();
                }
            });
        } else if ("splash".equals(mAdposition)) {//启动图
            aq.id(R.id.splash_ad_iv).visible();
            aq.id(R.id.splash_ad_iv).image(hzAdBean.src, false, true);
            activity.findViewById(R.id.nativeADContainer).setVisibility(View.VISIBLE);
        } else if ("focus".equals(mAdposition)) {
            aq.id(R.id.ad_text_mark_tv_one).visible();
            aq.id(R.id.focus_item_image).image(hzAdBean.src);
            aq.id(R.id.focus_item_tv).text(hzAdBean.title);
        } else if("homebanner".equals(mAdposition)){
            mView.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIs.getScreenWidth()*5/18);
            mView.findViewById(R.id.home_banner_ad).setLayoutParams(params);
            aq.id(R.id.text_banner_desc).text(hzAdBean.desc);
            aq.id(R.id.home_banner_ad).image(hzAdBean.src);
            aq.id(R.id.ad_text_mark_tv_banner).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        }else if ("exit".equals(mAdposition)) {
            aq.id(R.id.ad_tips_tv).visible();
            aq.id(R.id.iv_main).image(hzAdBean.src, false, true);
        } else {
            aq.id(R.id.text_desc_one).text(hzAdBean.desc);
            aq.id(R.id.text_title_one).text(hzAdBean.title);
            aq.id(R.id.ad_left_image_iv_one).image(hzAdBean.src);
            aq.id(R.id.ad_text_mark_tv_one).visibility(View.GONE);
            aq.id(R.id.server_ad_tips).visibility(View.VISIBLE);
        }
        if(!"focus".equals(mAdposition)){
            reportAdShow();
        }
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击响应
                if("0".equals(hzAdBean.action)){
                    CommonAdActionHandle.getInstance().downloadApp(activity, hzAdBean.app.package_name, hzAdBean.app.name, hzAdBean.app.icon_url, hzAdBean.url, hzAdBean.dp_url, hzAdBean.download_urls, hzAdBean.downloaded_urls, hzAdBean.installed_urls);
                }else if("1".equals(hzAdBean.action)){
                    if(!TextUtils.isEmpty(hzAdBean.dp_url) && !TextUtils.isEmpty(hzAdBean.app.package_name) && Utils.isAppInstalled(activity, hzAdBean.app.package_name)){
                        CommonAdActionHandle.getInstance().go2AppByDpLink(activity, hzAdBean.dp_url);
                        if(adClickDpReportUrls != null && adClickDpReportUrls.size() > 0){
                            for (int i=0;i<adClickDpReportUrls.size();i++){
                                if(!TextUtils.isEmpty(adClickDpReportUrls.get(i))){
                                    new ThirdAdReportTask(activity, adClickDpReportUrls.get(i)).start();
                                }
                            }
                        }
                    }else{
                        CommonAdActionHandle.getInstance().go2WebView(activity, hzAdBean.url);
                    }
                }
                reportAdClick();
            }
        });
    }

    private void reportAdShow() {
        if(adShowReportUrls != null && adShowReportUrls.size() > 0){
            for (int i=0;i<adShowReportUrls.size();i++){
                if(!TextUtils.isEmpty(adShowReportUrls.get(i))){
                    new ThirdAdReportTask(activity, adShowReportUrls.get(i)).start();
                }
            }
        }
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.HZSERVERAD_SHOW, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADSHOW);
        record.setAd_pro(SelfDataConstant.AD_HZ);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClick() {
        if(adClickReportUrls != null && adClickReportUrls.size() > 0){
            for (int i=0;i<adClickReportUrls.size();i++){
                if(!TextUtils.isEmpty(adClickReportUrls.get(i))){
                    new ThirdAdReportTask(activity, adClickReportUrls.get(i)).start();
                }
            }
        }
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.HZSERVERAD_CLICK, gdtPropertyMap);

        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(SelfDataConstant.AC_ADCLICK);
        record.setAd_pro(SelfDataConstant.AD_HZ);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }

    private void reportAdClose() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.HZSERVERAD_CLOSE, gdtPropertyMap);
    }
}
